package com.yuanyi.liu;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

class MyView extends View{
	private Paint mPaint;
	private Bitmap mBitmap;
	private float x1 = 0;
	private float y1 = 0;
	private ArrayList<Circle> circles;
	private boolean isMove = false;
	private boolean changeSize = false;
	public MyView(Context context){
		super(context);
	  
	    mPaint = new Paint();
	    mPaint.setAntiAlias(true);
	    mPaint.setARGB(0, 0xff, 0, 0);
	    mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.RGB_565);
	    circles = new ArrayList<Circle>();
	}
	 
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
	  
	    if(mBitmap != null)
	    	canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	public boolean onTouchEvent(MotionEvent event){
		
		float x = 0;
	    float y = 0;
	    x = event.getX();
	    y = event.getY();
	    Canvas canvas = new Canvas();  
	    canvas.setBitmap(mBitmap);  
	    mPaint.setARGB(100, 0xff, 0, 0);  
	  
	    canvas.drawColor(Color.BLACK);
	    boolean haveCircle = false;
	    switch(event.getAction()){
		    case MotionEvent.ACTION_DOWN:{
		    	if(circles.size()==0)
				{
					invalidate((int)(x1 - 40), (int)(y1 - 40), (int)(x1 + 40), (int)(y1 + 40));
					x1 = x;
				    y1 = y;
				    canvas.drawCircle(x, y, 40, mPaint);  
				    invalidate((int)(x - 40), (int)(y - 40), (int)(x + 40), (int)(y + 40));
				    circles.add(new Circle((int)x,(int)y,40));
				}else{
					int i= 0;
					for(i = 0; i < circles.size(); i++){
						
						if(isInCircle((int)x, (int)y, circles.get(i))){
							haveCircle = true;
							if(x<circles.get(i).x)
								isMove = true;
							else
								changeSize = true;
							//System.out.println(x+" "+y + circles.get(i).x+" "+ circles.get(i).y);
							break;
						}
					}
				}
		    	break;
		    }
		    case MotionEvent.ACTION_MOVE:
		    {
		    	if(circles.size()!=0){
		    		if(isMove){
						System.out.println("Move");
						Circle circle = circles.get(0);
						//invalidate((int)(circle.x - circle.r), (int)(circle.y - circle.r), (int)(circle.x + circle.r), (int)(circle.y + circle.r));
						circle.x += x - x1;
						circle.y += y - y1;
						canvas.drawCircle(circle.x, circle.y, circle.r, mPaint);  
						invalidate((int)(circle.x - circle.r), (int)(circle.y - circle.r), (int)(circle.x + circle.r), (int)(circle.y + circle.r));
					}
					
					if(changeSize){
						System.out.println("change");
						Circle circle = circles.get(0);
						invalidate((int)(circle.x - circle.r), (int)(circle.y - circle.r), (int)(circle.x + circle.r), (int)(circle.y + circle.r));
						int x_l = circle.x - circle.r;
						int y_l = circle.y - circle.r;
						circle.x = (int) ((x + x_l)/2);
						circle.y = (int) ((y + y_l)/2);
						circle.r = (int) ((x - x_l)/2);
						canvas.drawCircle(circle.x, circle.y, circle.r, mPaint);
						invalidate((int)(circle.x - circle.r), (int)(circle.y - circle.r), (int)(circle.x + circle.r), (int)(circle.y + circle.r));
					}
					
		    	}
		    	break;
		    }
		    case MotionEvent.ACTION_UP:
		    {
		    	isMove = false;
		    	changeSize = false;
		    	break;
		    }
	    }
	    x1 = x;
		y1 = y;
	    /*
		if(circles.size()==0)
		{
			invalidate((int)(x1 - 40), (int)(y1 - 40), (int)(x1 + 40), (int)(y1 + 40));
			x1 = x;
		    y1 = y;
		    canvas.drawCircle(x, y, 40, mPaint);  
		    invalidate((int)(x - 40), (int)(y - 40), (int)(x + 40), (int)(y + 40));
		    circles.add(new Circle((int)x,(int)y,40));
		}else{
			boolean haveCircle = false;
			int i;
			for(i = 0; i < circles.size(); i++){
				
				if(isInCircle((int)x, (int)y, circles.get(i))){
					haveCircle = true;
					System.out.println(x+" "+y + circles.get(i).x+" "+ circles.get(i).y);
					break;
				}
			}
			//如果触点在圆里面，则移动圆，(否则新建一个圆)
			if(haveCircle){
				Circle circle = circles.get(i);
				if(!isMove)
					isMove = true;
				else{
					invalidate((int)(circle.x - 40), (int)(circle.y - 40), (int)(circle.x + 40), (int)(circle.y + 40));
					circle.x += x - x1;
					circle.y += y - y1;
					x1 = x;
				    y1 = y;
				    canvas.drawCircle(circle.x, circle.y, 40, mPaint);  
				    invalidate((int)(circle.x - 40), (int)(circle.y - 40), (int)(circle.x + 40), (int)(circle.y + 40));
				}
				
//				invalidate((int)(x1 - 40), (int)(y1 - 40), (int)(x1 + 40), (int)(y1 + 40));
//				x1 = x;
//			    y1 = y;
//				canvas.drawCircle(x, y, 40, mPaint);  
//			    invalidate((int)(x - 40), (int)(y - 40), (int)(x + 40), (int)(y + 40));
//			    circles.get(i).x = (int)x;
//			    circles.get(i).y = (int)y;
			    
			}else{
				isMove = false;
			}
			
//			else if(false){
//				//invalidate((int)(x1 - 40), (int)(y1 - 40), (int)(x1 + 40), (int)(y1 + 40));
//			    canvas.drawCircle(x, y, 40, mPaint);  
//			    invalidate((int)(x - 40), (int)(y - 40), (int)(x + 40), (int)(y + 40));
//			    circles.add(new Circle((int)x,(int)y,40));
//			    x1 = x;
//			    y1 = y;
//			}
		}
		*/
		
	    return true;  

	}
	
	//判断一个点是否在一个圆内（是否在圆的外切正方形内）
	private boolean isInCircle(int x, int y, Circle c){
		if(x >= c.x-c.r && x <= c.x+c.r
				&& y >= c.y-c.r && y <= c.y+c.r){
			return true;
		}
		return false;
	}
}

class Circle{
	
	public Circle(int x, int y, int r){
		this.x = x;
		this.y = y;
		this.r = r;
	}
	int x;
	int y;
	int r;
}

